<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="咨询学生" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入咨询学生"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题状态" prop="wtStatus">
        <el-select v-model="queryParams.wtStatus" placeholder="请选择问题状态" filterable clearable>
          <el-option
            v-for="dict in dict.type.sxgc_wtzt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="导师" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入导师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sxgc:ssgt:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sxgc:ssgt:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sxgc:ssgt:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ssgtList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50" type="index" />
      <el-table-column label="咨询学生" align="center" prop="studentName" />
      <el-table-column label="咨询问题" align="center" prop="zxwt" />
      <el-table-column label="咨询时间" align="center" prop="zxsj" width="150"/>
      <el-table-column label="问题状态" align="center" prop="wtStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sxgc_wtzt" :value="scope.row.wtStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="导师" align="center" prop="teacherName" />
      <el-table-column label="回复时间" align="center" prop="hfsj" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.wtStatus === 'dhf'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sxgc:ssgt:edit']"
          >回复</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sxgc:ssgt:remove']"
          >删除</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['sxgc:ssgt:view']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改师生沟通对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" inline>
        <el-form-item label="咨询学生" prop="studentId">
          <el-select v-model="form.studentId" placeholder="请选择学生" filterable clearable disabled style="width: 240px;">
            <el-option
              v-for="item in studentSelectData"
              :key="item.studentId"
              :label="item.name"
              :value="item.studentId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="导师" prop="teacherId">
          <el-select v-model="form.teacherId" placeholder="请选择导师" filterable clearable disabled style="width: 240px;">
            <el-option
              v-for="item in teacherSelectData"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
              <span style="float: left">{{ item.nickName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phoneNumber }}</span></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="咨询问题" prop="zxwt">
          <el-input v-model="form.zxwt" type="textarea" rows="7" placeholder="请输入内容" :disabled="!this.isAdd" style="width: 600px;"/>
        </el-form-item>
        <el-form-item label="咨询时间" prop="zxsj" v-if="!isAdd">
          <el-date-picker clearable
                          :disabled="!this.isAdd"
                          style="width: 240px"
                          v-model="form.zxsj"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择咨询时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="问题状态" prop="wtStatus"  v-if="!isAdd">
          <el-select v-model="form.wtStatus" placeholder="请选择问题状态" filterable clearable disabled style="width: 240px;">
            <el-option
              v-for="dict in dict.type.sxgc_wtzt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="导师回复" prop="dshf" v-if="!isAdd">
          <el-input v-model="form.dshf" type="textarea" rows="7" placeholder="请输入内容" :disabled="this.isView" style="width: 600px;"/>
        </el-form-item>
        <el-form-item label="回复时间" prop="hfsj" v-if="this.isView">
          <el-date-picker clearable
                          :disabled="this.isView"
                          style="width: 240px"
                          v-model="form.hfsj"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择回复时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="this.isView" style="width: 600px;"/>
        </el-form-item>
        <el-form-item label="附件" prop="fj">
          <file-upload v-model="form.fj" :disabled="this.isView" :limit="10" style="width: 600px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer"  v-if="!this.isView">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSsgt, getSsgt, delSsgt, addSsgt, updateSsgt } from "@/api/sxgc/ssgt"
import {getUserSelect} from "@/api/system/user";
import {getStudentSelect, getUserInfoData} from "@/api/xxgl/xsxx";

export default {
  name: "Ssgt",
  dicts: ['sxgc_wtzt'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 是否添加
      isAdd: true,
      // 学生信息下拉框数据
      studentSelectData: [],
      // 导师信息下拉框数据
      teacherSelectData: [],
      // 当前用户ID
      currentUserId: this.$store.state.user.id,
      // 当前用户角色
      currentUserRole: this.$store.state.user.roles[0],
      // 总条数
      total: 0,
      // 师生沟通表格数据
      ssgtList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否禁用查看按钮
      isView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        studentName: null,
        wtStatus: null,
        teacherId: null,
        teacherName: null,
        glId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentId: [{ required: true, message: "请选择咨询学生", trigger: "change" }],
        teacherId: [{ required: true, message: "请选择导师", trigger: "change" }],
        zxwt: [{ required: true, message: "请输入咨询问题", trigger: "blur" }],
        zxsj: [{ required: true, message: "请选择咨询时间", trigger: "change" }],
        wtStatus: [{ required: true, message: "请选择问题状态", trigger: "change" }],
        dshf: [{ required: true, message: "请输入导师回复", trigger: "blur" }],
        hfsj: [{ required: true, message: "请选择回复时间", trigger: "change" }],
      }
    }
  },
  created() {
    this.getList()
    this.getUserSelectData()
  },
  methods: {
    /** 查询师生沟通列表 */
    getList() {
      this.loading = true
      if(this.currentUserRole === 'student'){
        this.queryParams.glId = this.currentUserId
      }else if(this.currentUserRole === 'teacher'){
        this.queryParams.teacherId = this.currentUserId
      }
      listSsgt(this.queryParams).then(response => {
        this.ssgtList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        zxId: null,
        studentId: null,
        zxwt: null,
        fj: null,
        wtStatus: null,
        teacherId: null,
        dshf: null,
        zxsj: null,
        hfsj: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.zxId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查询用户下拉框数据 */
    getUserSelectData() {
      /*  roleId  2:学生  3:老师 */
      /* 查询角色为老师下拉框内容*/
      getUserSelect(3).then(response => {
        this.teacherSelectData = response.data
      })
      /* 查询学生信息表 学生下拉框内容*/
      getStudentSelect().then(response => {
        this.studentSelectData = response.data
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.isView = false
      this.isAdd = true
      /* 根据当前登录用户查询个人信息*/
      getUserInfoData(this.currentUserId).then(res => {
        this.form.studentId = res.data.studentId
        if(res.data.teacherId !== null){
          this.form.teacherId = res.data.teacherId
          this.open = true
        }else{
          this.$modal.msgWarning("实习信息中尚未绑定导师，请先完善信息！")
        }
      });
      this.form.wtStatus='dhf'
      this.title = "添加"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const zxId = row.zxId || this.ids
      getSsgt(zxId).then(response => {
        this.form = response.data
        this.isView = false
        this.isAdd = false
        this.open = true
        this.title = "回复"
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset()
      const zxId = row.zxId || this.ids
      getSsgt(zxId).then(response => {
        this.form = response.data
        this.isView = true
        this.isAdd = false
        this.open = true
        this.title = "查看"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.zxId != null) {
            this.form.hfsj = this.$dayjs().format('YYYY-MM-DD HH:mm:ss')
            this.form.wtStatus = "yhf"
            updateSsgt(this.form).then(response => {
              this.$modal.msgSuccess("回复成功")
              this.open = false
              this.getList()
            })
          } else {
            this.form.zxsj = this.$dayjs().format('YYYY-MM-DD HH:mm:ss')
            addSsgt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const zxIds = row.zxId || this.ids
      this.$modal.confirm('是否确认删除?').then(function() {
        return delSsgt(zxIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sxgc/ssgt/export', {
        ...this.queryParams
      }, `师生沟通${this.$dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`)
    }
  }
}
</script>
